from threading import Thread, Lock
import time
import cv2
from .cv_image import add_detectedlane_image,add_object_feature_image,detect_red_object,detect_rectangle,detect_yellow_flower,detect_circles
from .cv_image_controller import  ObstacleDetector
from .cv_enhanced_vision_controller import EnhancedVisionCarController

class CameraStream:
    OPENCV_NONE=0 
    OPENCV_LANE=1 
    OPENCV_OBJECT_FEATURE=2 
    OPENCV_OBJECT_RED=3 
    OPENCV_OBJECT_RECTANGLE=4 
    OPENCV_YELLOW_FLOWER=5
    OPENCV_OBJECT_CIRCLE=6
    OPENCV_OBSTACLE_DETECTOR=7
    OPENCV_ENHANCED_VISION=8   
    
    def __init__(self, width=1920,height=1080):
        src=0
        self.stream = cv2.VideoCapture(src)
        self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, width)  # 1920
        self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT,height)  # 1080
   
        (self.grabbed, self.frame) = self.stream.read()
        self.started = False
        self.read_lock = Lock()
        self.cur_opencv_action= CameraStream.OPENCV_NONE

    def start(self):
        if self.started:
            print("already started!!")
            return None
        self.started = True
        self.thread = Thread(target=self.update, args=())
        self.daemon=True
        self.thread.start()
        return self

    def update(self):
        obs_detector=ObstacleDetector()
        enhanced_vision_controller=EnhancedVisionCarController()
        while self.started:
            time.sleep(0.033)
            (grabbed, frame) = self.stream.read()
           
            result=frame
            #  import the opencv mothods to process the image from opencv_image 
            if self.cur_opencv_action==CameraStream.OPENCV_LANE:
                result=add_detectedlane_image(result)
            elif self.cur_opencv_action==CameraStream.OPENCV_OBJECT_RED:
                result=detect_red_object(result)
            elif self.cur_opencv_action==CameraStream.OPENCV_OBJECT_RECTANGLE:
                result=detect_rectangle(result)
            elif self.cur_opencv_action==CameraStream.OPENCV_OBJECT_CIRCLE:
                result=detect_circles(result)
            elif self.cur_opencv_action==CameraStream.OPENCV_YELLOW_FLOWER:
                result=detect_yellow_flower(result)    
            elif self.cur_opencv_action==CameraStream.OPENCV_OBSTACLE_DETECTOR:
                result= obs_detector.process_frame(result)   
            elif self.cur_opencv_action==CameraStream.OPENCV_ENHANCED_VISION:
                result=  enhanced_vision_controller.process_frame(result)       
            elif self.cur_opencv_action==CameraStream.OPENCV_NONE:
                result=result
           
            # 2 display current time 
            # result=add_time_to_image(result)
            
            self.read_lock.acquire()
            self.grabbed, self.frame = grabbed, result
            self.read_lock.release()

    def read(self):
        self.read_lock.acquire()
        frame = self.frame.copy()
        self.read_lock.release()
        return frame

    def stop(self):
        self.started = False
        self.thread.join()

    def __exit__(self, exc_type, exc_value, traceback):
        self.stream.release()